import React from 'react'
import {HashRouter, Route, Switch, Redirect} from 'react-router-dom';
import {message} from 'antd';
import App from './App'
import Login from './pages/Login/login'
import Admin from './admin'



import Home from './pages/home/index'
import User from './pages/user_mange/user/index'
import Role from './pages/user_mange/role/index'
import Workshop from './pages/user_mange/workshop/index'
import ErrorPage from './error_pages/404'
import {fetch, post} from './my_axios/api'

const routerList = [
    {
        path: "/admin/home",
        name: "Home",
        component: Home
    },
    {
        path: "/admin/user/list",
        name: "User",
        component: User
    },
    {
        path: "/admin/role/list",
        name: "Role",
        component: Role
    },
    {
        path: "/admin/workshop/list",
        name: "Workshop",
        component: Workshop
    },
];
export default class IRouter extends React.Component {
    state={
        isRight:false
    };

    // verifyToken(){
    //     fetch('user/current').then(res => {
    //         if (res.status === 'success') {
    //             console.log("正确");
    //            this.setState({
    //                isRight:true
    //            })
    //         } else {
    //             message.error(res.data.message);
    //             this.setState({
    //                 isRight:false
    //             })
    //         }
    //     }).catch(err => {
    //         console.log(err);
    //     });
    // }

    /**
     * 路由拦截
     * @param props
     * @returns {XML}
     * @constructor
     */
    RouteFallback = (props) => {
        console.log('route fallback with location: ', props.location);
        if(props.location.pathname==='/'){
            return <Redirect from="/" to={{
                pathname: '/admin/home',
                from: props.location
            }}/>
        }else if(props.location.pathname==='/123'){//模拟错误token
            window.localStorage.setItem("token",'121asdasdasasdasdasasdjgaksdh');
            return <Redirect from="/" to={{
                pathname: '/login',
                from: props.location
            }}/>
        } else{
            return <Redirect from="/" to={{
                pathname: '/login',
                from: props.location
            }}/>
        }
    };
    render() {
        return (
            <HashRouter>
                <App>
                    <Switch>
                        <Route path="/login" component={Login}/>
                        <Route path="/admin" render={() =>
                            <Admin>
                                <Switch>
                                    {routerList.map((item, index) =>{
                                                       return window.localStorage.getItem('token')!==null?<Route key={index} path={item.path} exact render={props =>
                                                               <item.component {...props} />}/>:  <Route key={index} render={props=>{
                                                                   return(
                                                                       <Redirect to={{pathname: '/login', state: { from: props.location }}}/>
                                                                   )
                                                               }
                                                       }/>
                                                     })}
                                    <Route component={ErrorPage}/>
                                </Switch>
                            </Admin>
                        }/>
                        <Route component={this.RouteFallback}/>
                    </Switch>
                </App>
            </HashRouter>
        );
    }
}